<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB, Session;

class GroupController extends Controller
{
    /**
     * Display a listing of the resource.
     *显示分组列表
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //查询所有分组信息
        $groups = DB::table("group_rule")->get();
        $rules = DB::table("rule")->get();
        //加载模板显示
        return view("admin.group.index", ["groups" => $groups, "rules" => $rules]);
    }

    //设置分组的权限
    public function setRule(Request $request) 
    {
        //判断是添加还是删除一个权限
        $rules = explode(",", DB::table("group_rule")->where("id", $request->get("id"))->pluck("rules"));
        if (false != $key = array_search($request->get("rule"), $rules))
        {
            unset($rules[$key]);
        } else
        {
            array_push($rules, $request->get("rule"));
        }
        $res = DB::table("group_rule")->where("id", $request->get("id"))->update(["rules" => join(",", $rules)]);
        //修改该分组的权限集合
        if (false != $res)
        {
            return response()->json(["status" => 1, "info" => "修改分组权限成功"]);
        } else
        {
            return response()->json(["status" => 0, "info" => "修改分组权限失败"]);
        }
    }
    
    /**
     * Show the form for creating a new resource.
     *显示一个新建资源的表单
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //查询所有的权限
        $rules = DB::table("rule")->get();
        //显示模板
        return view("admin.group.create", compact("rules"));
    }

    /**
     * Store a newly created resource in storage.
     *存储一个新建的资源
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //接收数据
        $data = $request->except("_token");
//        dd($data);
        if(isset($data['rules'])){
            $data["rules"] = join(",", $data["rules"]);
        }
        //存入数据库
        if (false !== $insertID = DB::table("group_rule")->insertGetId($data))
        {
            return redirect("/tips")->with(["info" => "Add Ok.ID:".$insertID, "url" => "/admin/group"]);
        } else 
        {
            return redirect("/tips")->with(["info" => "failed", "url" => "/admin/group"]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *显示一个编辑资源的表单            
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //查询该分组的信息
        $group = DB::table("group_rule")->where("id", $id)->first();
        //查询所有的权限
        $rules = DB::table("rule")->get();
        //发送模板显示
        return view("admin.group.edit", ["group" => $group, "rules" => $rules]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //接收数据
        $data = $request->except("_token", "_method");
        $data["rules"] = join(",", $data["rules"]);
//        dd($data);
        //更新一条记录
        if (false !== DB::table("group_rule")->where("id", $id)->update($data))
        {
            return redirect("/tips")->with(["info" => "Update OK!", "url" => "/admin/group"]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *删除一个资源的方法
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除这个分组
        DB::table("group_rule")->where("id", $id)->delete();
        //删除该分组下的所有用户
        $users = DB::table("user_group")->where("groupid", $id)->get();
        foreach ($users as $user)
        {
            DB::table("user")->where("uid", $user->uid)->delete();
        }
        return back();
    }
}
